<template>
  <div id="blog-edit">
    <Header></Header>
    
    <div class="m-content">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        
        <el-form-item label="摘要" prop="desc">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>
        <!--prop="content"啥意思-->
        <!--绑定markdown编辑器-->
        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="ruleForm.content"></mavon-editor>
        </el-form-item>
        <el-form-item>
          <el-button v-show="editORcre" type="primary" @click="submitForm('ruleForm')">创建博客</el-button>
          <el-button v-show="!editORcre" type="primary" @click="submitForm('ruleForm')">修改博客</el-button>
          <el-button @click="resetForm('ruleForm')">取消创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header";

export default {
  name: "BlogEdit",
  components: {Header},
  props:['blogId'],
  data() {
    return {
      editORcre:true,
      ruleForm: {
        id:'',
        title:'',
        description: '',
        content:'',
      },
      rules: {
        title: [
          { required: true, message: '请输入博客名称', trigger: 'blur' },
          { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请填写摘要', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '博客内容不能空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    //创建博客
    submitForm(formName) {
      const _this= this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.$axios.post('/blog/edit',this.ruleForm,{
            headers:{'Authorization':localStorage.getItem('token')}
          }).then(resp=>{
            this.$alert('操作成功', {
              confirmButtonText: '确定',
              callback: action => {
                _this.$router.push('/blog/all')
              }
            });
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //取消创建
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  //如果加载时候就有id了，表示这是修改，通过异步请求后端找到对应的blog返回给前端。
  created() {
    const _this=this;
    const blogId=this.$route.params.blogId;
    if(blogId){
      this.editORcre=false;
      _this.$axios.get('/blog/'+blogId).then(resp=>{
        const blog=resp.data.data;
        //加上id是因为后端操作：添加时blogId=null，修改时blogId有值，如果id有值就表示是修改，
        this.ruleForm.id=blog.id;
        this.ruleForm.title=blog.title;
        this.ruleForm.description=blog.description;
        this.ruleForm.content=blog.content;
        console.log(blog)
      })
    }
  }
}
</script>

<style scoped>
  .m-content{
    text-align: center;
  }
</style>